package com.wenbing.miio.mi.device.remote;

import com.wenbing.miio.util.TimeUtil;

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.util.concurrent.TimeUnit;

/**
 * @author wen bing
 * @projectName: miio
 * @package: com.wenbing.miio.mi.device.remote
 * @className: RemoteClock
 * @date 2024/09/21 14:28
 **/
public class RemoteClock extends Clock {
    private long startTime = 0;
    private boolean sync = false;
    /**
     * 时钟是否已同步
     * @return
     */
    public boolean isSynced() {
        return this.sync;
    }

    /**
     * 同步时钟
     */
    public void sync(long timestamp) {
        this.startTime = TimeUtil.now(TimeUnit.SECONDS) - timestamp;
        this.sync = true;
    }

    /**
     * 时钟重置
     */
    public void reset() {
        this.startTime = 0;
        this.sync = false;
    }

    @Override
    public ZoneId getZone() {
        return null;
    }

    @Override
    public Clock withZone(ZoneId zone) {
        return null;
    }

    @Override
    public Instant instant() {
        if (!isSynced()) {
            return Instant.ofEpochSecond(0xffffffff);
        }
        return Instant.ofEpochSecond(TimeUtil.now(TimeUnit.SECONDS) - startTime);
    }
}
